﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FooterViewModel.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Footer view model
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.MVVM.ViewModels
{
    #region Using Directives

    using System;
    using System.Collections.ObjectModel;
    using System.Reflection;
    using System.Windows.Browser;
    using System.Windows.Input;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;
    using Globals;
    using Logging;

    using Spenta.StreetCare.Silverlight.MVVM.Messages;

    using StreetCareAPI;

    #endregion

    /// <summary>
    /// Footer view model
    /// </summary>
    public class FooterViewModel : BaseViewModel
    {
        #region Private members

        /// <summary>
        /// Private store for comments
        /// </summary>
        private ObservableCollection<Comment> comments;

        #endregion

        #region Class Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="FooterViewModel"/> class.
        /// </summary>
        public FooterViewModel()
        {
            if (HtmlPage.IsEnabled)
            {
                ApiClient.GetLatestCommentsCompleted += this.GetLatestCommentsCompleted;
                ApiClient.GetLatestCommentsAsync(Parameters.CustomerID);

                Messenger.Default.Register<NewCommentCompletedMessage>(this, this.InsertComment);
            }
        }

        #endregion

        #region ViewModel Properties

        /// <summary>
        /// Gets or sets the comments.
        /// </summary>
        /// <value>The comments.</value>
        public ObservableCollection<Comment> Comments
        {
            get
            {
                return this.comments;
            }

            set 
            { 
                this.comments = value; 
                RaisePropertyChanged("Comments");
            }
        }

        /// <summary>
        /// Gets the locate report command reference
        /// </summary>
        /// <value>The locate report command.</value>
        public ICommand LocateReportCommand
        {
            get
            {
                return new RelayCommand<Report>(this.LocateReport);
            }
        }

        /// <summary>
        /// Gets the more comments command reference
        /// </summary>
        /// <value>The more comments command.</value>
        public ICommand MoreCommentsCommand
        {
            get
            {
                return new RelayCommand(this.MoreComments);
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Inserts the comment.
        /// </summary>
        /// <param name="message">The message.</param>
        public void InsertComment(NewCommentCompletedMessage message)
        {
            // TODO: Update all comment dates
            this.Comments.Insert(0, message.Comment);
            this.RaisePropertyChanged("Comments");
        }

        /// <summary>
        /// Handles the create report command.
        /// </summary>
        /// <param name="report">The report.</param>
        public void LocateReport(Report report)
        {
            Messenger.Default.Send(new OpenReportMessage { Report = report });
        }

        /// <summary>
        /// Handles the create report command.
        /// </summary>
        public void MoreComments()
        {
            // TODO: decide destination url
            HtmlPage.Window.Navigate(new Uri(string.Empty, UriKind.Relative));
        }

        #endregion

        #region Event Methods

        /// <summary>
        /// Handles the get comments latest completed event.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Spenta.StreetCare.Silverlight.StreetCareAPI.GetLatestCommentsCompletedEventArgs"/> instance containing the event data.</param>
        private void GetLatestCommentsCompleted(object sender, GetLatestCommentsCompletedEventArgs e)
        {
            try
            {
                if (e.Result.IsFault)
                {
                    LogHelper.Logger.WriteError("Error getting comments", e.Result.Fault.Exception);
                    return;
                }
            }
            catch (TargetInvocationException ex)
            {
                LogHelper.Logger.WriteError("Error getting comments", ex.InnerException ?? ex);
                return;
            }

            this.Comments = e.Result.Comments;
        }

        #endregion
    }
}
